﻿//-----------------------------------------------------------------------------------------------
// <copyright file="ReflectionHelper.cs" company="VSSD" author="VSSD-KT" initializationUser="TriDM">
//     Copyright © VietNam Securities Software Development (VSSD) 2009.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------------------------------

namespace Lapas.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using CrystalDecisions.CrystalReports.Engine;
    using System.ComponentModel;

    /// <summary>
    /// ReflectionHelper class
    /// </summary>
    public partial class ReflectionHelper
    {
        /// <summary>
        /// Hàm load thông tin constructor của 1 class có đường dẫn nameSpaceString
        /// </summary>
        /// <param name="nameSpacePath">Đường dẫn chi tiết đến file</param>
        /// <returns>Trả về đối tượng ConstructorInfo</returns>
        public static ConstructorInfo Load(string nameSpacePath)
        {
            if (nameSpacePath == null)
            {
                return null;
            }

            Type controlType = Type.GetType(nameSpacePath);
            if (controlType == null)
            {
                throw new Exception(string.Format("The Type {0} could not be loaded by the Oheboca.Loader", nameSpacePath));
            }

            return controlType.GetConstructor(new Type[0]);
        }

        /// <summary>
        /// Load 1 report class từ 1 nameSpacePath
        /// </summary>
        /// <param name="nameSpacePath">Đường dẫn chi tiết đến</param>
        /// <returns>Đối tượng ReportClass</returns>
        public static ReportClass GetReportClass(string nameSpacePath)
        {
            return Load(nameSpacePath).Invoke(new object[] { }) as ReportClass;
        }

        public static List<ReportClass> GetReportParameters()
        {
            ReportClass c = new ReportClass();
            //c.DataDefinition.ParameterFields
            return new List<ReportClass>();
        }

        public class CloneObject<T>
        {
            public T Clone<T>(T sourceObj, T returnObj)
            {
                PropertyDescriptorCollection listProInfor = TypeDescriptor.GetProperties(sourceObj.GetType());
                foreach (PropertyDescriptor item in listProInfor)
                {
                    if (item.IsBrowsable && (!item.IsReadOnly))
                    {
                        returnObj.GetType().GetProperty(item.Name).SetValue(returnObj,
                                sourceObj.GetType().GetProperty(item.Name).GetValue(sourceObj, null),
                                null);
                    }
                }

                return returnObj;
            }
        }
    }
}
